package org.example.page;

import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.example.internet.MessageSender;
import org.example.param.Parameter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

@Log4j2
public class ChatPanel extends JPanel implements ActionListener {
    private CardLayout cardLayout;
    private JPanel parentPanel;

    private JTextArea chatArea;
    private JTextField inputField;
    private JButton sendBtn;

    private JPanel sendFieldAndBtnPanel;
    private JPanel titlePanel;

    private MessageSender messageSender;


    public ChatPanel(CardLayout cardLayout, JPanel parentPanel) {
        this.cardLayout = cardLayout;
        this.parentPanel = parentPanel;

        panelInit();
        //聊天空间初始化
        chatAreaInit();

        //sendFieldAndBtn初始化
        sendFieldAndBtnInit();

        returnBtnInit();


        chatInit();


    }

    private void chatInit() {
        messageSender = new MessageSender(chatArea);
        messageSender.start();
    }

    private void returnBtnInit() {
        titlePanel = new JPanel();
        titlePanel.setLayout(new BorderLayout());
        JButton returnBtn = new JButton(Parameter.RETURN_BTN);
        returnBtn.addActionListener(this);
        //将按钮添加进titlePanel
        titlePanel.add(returnBtn,BorderLayout.WEST);

        //将titlePanel添加到当前页面
        this.add(titlePanel,BorderLayout.NORTH);

    }

    private void sendFieldAndBtnInit() {
        sendFieldAndBtnPanel = new JPanel();
        sendFieldAndBtnPanel.setLayout(new BorderLayout());

        //输入初始化
        inputFieldInit();

        //发送按钮初始化
        inputBtnInit();

        sendFieldAndBtnPanel.add(inputField,BorderLayout.CENTER);
        sendFieldAndBtnPanel.add(sendBtn,BorderLayout.EAST);

        this.add(sendFieldAndBtnPanel,BorderLayout.SOUTH);
    }

    private void inputBtnInit() {
        // 创建发送按钮
        sendBtn = new JButton("发送");
        sendBtn.addActionListener(this);
//        sendBtn.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//
//            }
//        });
//        this.add(sendButton, BorderLayout.EAST);
    }

    private void inputFieldInit() {
        // 创建输入区域
        inputField = new JTextField();
        inputField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 发送消息
                String message = inputField.getText();
                sendBtnOnClick();
                inputField.setText("");
            }
        });

        //将输入添加到panel中
//        this.add(inputField, BorderLayout.SOUTH);

    }

    private void chatAreaInit() {
        // 创建聊天记录区域
        chatArea = new JTextArea();
        chatArea.setEditable(false);
        chatArea.setLineWrap(true);
        JScrollPane scrollPane = new JScrollPane(chatArea);
        this.add(scrollPane, BorderLayout.CENTER);
    }

    private void panelInit() {
        // 初始化聊天界面
        this.setLayout(new BorderLayout());


    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof JButton){
            JButton btn = (JButton) e.getSource();
            String btnText = btn.getText();
            switch (btnText) {
                case Parameter.SEND_BTN:
                    log.info("开始发送数据");
                    sendBtnOnClick();
                    break;
                case Parameter.RETURN_BTN:
                    //返回主页面
                    cardLayout.show(parentPanel, "mainPanel");

                    break;
            }

        }
    }

    private void sendBtnOnClick() {
        String message = inputField.getText();
        // 发送消息
        messageSender.sendMessage(message);
        //展示消息
        showMessage(message);


    }

    private void showMessage(String message) {

        if (message.isEmpty() || "".equals(message.trim())){
            log.info("数据为空，不展示，不发送");
            return;
        }
        chatArea.append("我：" + message + "\n");
        inputField.setText("");
    }
}
